package com.itheima.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.returnReason;
import com.itheima.dto.returnReasonDto;
import com.itheima.mapper.OmsOrderReturnMapper;
import com.itheima.pojo.OmsOrderReturnReason;
import com.itheima.vo.MinioVo;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 芝士雪豹
 * @create 2022/10/15/17:22
 */
@DubboService
public class OmsOrderReturnApIImpl implements OmsOrderReturnApi {
    @Autowired
    private OmsOrderReturnMapper mapper;

    @Override
    public MinioVo create(returnReasonDto dto) {
        LambdaUpdateWrapper<OmsOrderReturnReason> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(dto.getId() != null, OmsOrderReturnReason::getId, dto.getId())
                .set(dto.getCreateTime() != null, OmsOrderReturnReason::getCreateTime, dto.getCreateTime())
                .set(dto.getName() != null, OmsOrderReturnReason::getName, dto.getName())
                .set(dto.getSort() != null, OmsOrderReturnReason::getSort, dto.getSort())
                .set(dto.getStatus() != null, OmsOrderReturnReason::getStatus, dto.getStatus());
        int update = mapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update != 0) {
            vo.setCode(200);
            vo.setDate(null);
            vo.setMessage("修改成功");
            return vo;
        }
        vo.setCode(500);
        vo.setDate(null);
        vo.setMessage("暂无数据修改");
        return vo;

    }

    @Override
    public MinioVo delete(Integer[] ids) {
        MinioVo vo = new MinioVo();
        for (Integer id : ids) {
            int deleteById = mapper.deleteById(id);
            if (deleteById == 0) {
                vo.setCode(500);
                vo.setDate(null);
                vo.setMessage("暂无数据修改");
                return vo;
            }
        }
        vo.setCode(200);
        vo.setDate(null);
        vo.setMessage("数据修改成功");
        return vo;
    }

    @Override
    public PageResult list(Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
        Page pageInfo = mapper.selectPage(page, wrapper);
        return new PageResult<>(Convert.toInt(pageInfo.getCurrent()), Convert.toInt(pageInfo.getSize()), Convert.toInt(pageInfo.getTotal()), Convert.toInt(pageInfo.getCurrent()), pageInfo.getRecords());

    }

    @Override
    public MinioVo updateStatus(Integer[] ids, Integer status) {
        MinioVo vo = new MinioVo();
        LambdaUpdateWrapper<OmsOrderReturnReason> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(ids != null, OmsOrderReturnReason::getId, ids).
                set(status != null, OmsOrderReturnReason::getStatus, status);
        int update = mapper.update(null, wrapper);
        if (update == 0) {
            vo.setCode(500);
            vo.setDate(null);
            vo.setMessage("暂无数据修改");
            return vo;
        }
        vo.setCode(200);
        vo.setDate(null);
        vo.setMessage("数据修改成功");
        return vo;

    }

    @Override
    public MinioVo updateById(Integer id, returnReason dto) {
        MinioVo vo = new MinioVo();
        LambdaUpdateWrapper<OmsOrderReturnReason> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(id!=null,OmsOrderReturnReason::getId,id)
                .set(dto.getCreateTime()!=null,OmsOrderReturnReason::getCreateTime,dto.getCreateTime())
                .set(dto.getSort()!=null,OmsOrderReturnReason::getSort,dto.getSort())
                .set(dto.getStatus()!=null,OmsOrderReturnReason::getStatus,dto.getStatus());
        int update = mapper.update(null, wrapper);
        if (update == 0) {
            vo.setCode(500);
            vo.setDate(null);
            vo.setMessage("暂无数据修改");
            return vo;
        }
        vo.setCode(200);
        vo.setDate(null);
        vo.setMessage("数据修改成功");
        return vo;
    }

    @Override
    public MinioVo findOne(Integer id) {
        OmsOrderReturnReason reason = mapper.selectById(id);
        MinioVo vo = new MinioVo();
        if (reason!=null){
            vo.setCode(200);
            vo.setDate(reason);
            vo.setMessage("获取数据成功");
            return vo;
        }
        vo.setCode(500);
        vo.setDate(null);
        vo.setMessage("暂无数据修改");
        return vo;
    }
}
